package cn.myweixin.controller;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import cn.myweixin.po.Notice;
import cn.myweixin.po.Trainee;
import cn.myweixin.service.impl.NoticeImpl;
import cn.myweixin.service.impl.TraineeImpl;

@Controller
@RequestMapping("/notice")
public class NoticeController {

	
	@Autowired
	 private TraineeImpl traineeImpl;
	@Autowired
	private NoticeImpl noticeImpl;
	
	@RequestMapping("/noticelist")
	public ModelAndView noticelist(HttpServletRequest request) throws Exception{
		
		List<Notice> notices=noticeImpl.findALLByNotice();
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("noticesList", notices);
		modelAndView.setViewName("notices/noticesList");
		return modelAndView;

	}
	
	@RequestMapping("/querysigns")
	public ModelAndView  querysigns(HttpServletRequest request) throws Exception{
		request.setCharacterEncoding("utf-8");
		 HttpSession session=request.getSession();
		Cookie[] cookies=request.getCookies();
		if(cookies!=null){
		for (Cookie cookie : cookies) {
			if(cookie.getName().equals("vx_openid")){
				    String id=cookie.getValue();
				    Trainee trainee= traineeImpl.selectByPrimaryKey(id);
				    ModelAndView modelAndView=new ModelAndView();
				    if(trainee!=null){
				       
				        session.setAttribute("openid", id);//cookie中找到的openid存入session中
				    	List<Notice> noticesExpect=noticeImpl.findListExpect(id);
				    	List<Notice> noticesExists=noticeImpl.findListExists(id);
				    	modelAndView.addObject("noticesExpect", noticesExpect);
				    	modelAndView.addObject("noticesExists", noticesExists);
				    	modelAndView.setViewName("notices/sign");
				    	return modelAndView;
				    }
				    else{
				    	
				    	return new ModelAndView(new RedirectView("user/register.action"));
				    	
				    }
				}
			}
		}
		
		return new ModelAndView(new RedirectView("user/cookievalidate.action"));
		
		
	}
	
	
	
}
